import Vue from 'vue'
import Router from 'vue-router'
import Cookies from 'js-cookie';
import store from './store'

import Index from './views/Index.vue'
import upload from './components/qn-upload.vue'
import My from './views/My.vue'
import Course from './views/Course.vue'
import Expert from './views/Expert.vue'
import Meeting from './views/Meeting.vue'
import Resource from './views/Resource.vue'

import MyInfo from './views/MyInfo.vue'
import MyOrder from './views/MyOrder.vue'
import MyCourse from './views/MyCourse.vue'
import MyFile from './views/MyFile.vue'
import MyInto from './views/MyInto.vue'
import MyCollect from './views/MyCollect.vue'
import MyVideo from './views/MyVideo.vue'

import ExpertReview from './views/ExpertReview.vue'

import More from './views/More.vue'

import Show from './views/Show.vue'

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/index',
      name: 'index',
      component: Index
    },
    {
      path: '/upload',
      name: 'upload',
      component: upload
    },
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/my',
      name: 'my',
      component: My,
      redirect: '/my/myInfo',
      children: [{
          path: "myInfo",
          name: "myInfo",
          component: MyInfo
        },
        {
          path: "myOrder",
          name: "myOrder",
          component: MyOrder
        },
        {
          path: "myCourse",
          name: "myCourse",
          component: MyCourse
        },
        {
          path: "myFile",
          name: "myFile",
          component: MyFile
        },
        {
          path: "myInto",
          name: "myInto",
          component: MyInto
        },
        {
          path: "myCollect",
          name: "myCollect",
          component: MyCollect
        },
        {
          path: "myVideo/:cid?",
          name: "myVideo",
          component: MyVideo
        }

      ]
    },
    {
      path: '/course',
      name: 'course',
      component: Course
    },
    {
      path: '/expert',
      name: 'expert',
      component: Expert
    },
    {
      path: '/meeting',
      name: 'meeting',
      component: Meeting
    },
    {
      path: '/resource',
      name: 'resource',
      component: Resource
    },
    {
      path: '/expert-review/:uid',
      name: 'expert-review',
      component: ExpertReview
    }, {
      path: '/more/:uid/:ftype',
      name: 'more',
      component: More
    },
    {
      path: '/show/:cid/:uid?',
      name: 'video-show',
      component: Show
    },
    {
      path: '/error',
      name: 'error',
      title: '出错了',
      component: () =>
        import ('@/views/Error.vue')
    },
  ]
})

router.beforeEach((to, from, next) => {
  if (to.name == 'my' || to.matched[0].name == 'my' || to.name == 'show') {
    if (Cookies.get('userinfo')) {
      next();
    } else {
      if (!from.name) {
        next('/')
      }
      store.dispatch("handleLoginTypeChange", 1);
      store.dispatch("handleLoginBoxChange", true);
    }
  } else {
    next();
  }
})
export default router;